/*
 * SRM 233 Div 1 Pro 2
 */

import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;


public class SmartWordToy
{
    static int[] scales = {26*26*26, 26*26, 26, 1};
    
    /* Caculate the int from string. */
    int getV (char[] array) {
	int ans = 0;
	for (int i = 0; i < 4; ++i) 
	    ans += (array[i]-'a')*scales[i];
	return ans;
    }
    
    int getIndex (int d, int i) {
	if (i != 0)
	    d %= scales[i-1];
	return d/scales[i];
    }
    
    /* Advanced the ith char. */
    int getNext1 (int d, int i) {
	int k = getIndex (d, i);
	if (k == 25)
	    return d - 25 * scales[i];
	return d + scales[i];
    }
    
    int getNext2 (int d, int i) {
	int k = getIndex (d, i);
	if (k == 0)
	    return d + 25 * scales[i];
	return d - scales[i];
    }
    

    public int minPresses(String start, String finish, String[] forbid)
    {
	int s = getV (start.toCharArray ()), f = getV(finish.toCharArray ());
	
	/* Use BFS. */
	/* queue for value. */
	LinkedList<Integer> queueV = new LinkedList<Integer> ();
	/* queue for level. */
	LinkedList<Integer> queueL = new LinkedList<Integer> ();
	/* hash set for forbiden string. */
	HashSet<Integer> fuck = new HashSet<Integer> ();
	/* hash set for visited string. */
	HashSet<Integer> visited = new HashSet<Integer> ();
	for (String tmp:forbid) {
	    String[] tmps = tmp.split ("\\s+");
	    int[] k = new int[4];
	    char[] arr = new char[4];
	    while (true) {
		int index = 3;
		while (index >= 0) {
		    if (k[index] < tmps[index].length ())
			break;
		    --index;
		}
		if (index < 0)
		    break;
		if (index <= 2)
		    Arrays.fill (k, index + 1, 4, 0);
		for (int j = index; j < 4; ++j)
		    arr[j] = tmps[j].charAt (k[j]);
		k[index]++;
		fuck.add (getV (arr));
	    }
	}
	
	if (fuck.contains (s) || fuck.contains (f))
	    return -1;
	queueV.addLast (s);
	visited.add (s);
	queueL.addLast (0);
	
	/* BFS. */
	while (!queueV.isEmpty ()) {
	    int top = queueV.removeFirst ();
	    int l = queueL.removeFirst ();
	    if (top == f)
		return l;
	    for (int i = 0; i < 4; ++i) {
		int k = getNext1 (top, i);
		if (!fuck.contains (k) && !visited.contains (k)) {
		    queueV.addLast (k);
		    visited.add (k);
		    queueL.addLast (l+1);
		}
		k = getNext2 (top, i);
		if (!fuck.contains (k) && !visited.contains (k)) {
		    queueV.addLast (k);
		    visited.add (k);
		    queueL.addLast (l+1);
		}
	    }
	    
	}
	return -1;
	
    }
    
    <%:testing-code%>
}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!